import { getElement, checkEmpty } from "./deal"
export default {
  install(app, options) {
    app.directive("check", {
      mounted: function (el, binding) {
        console.log(">>>",el);
        // getElement(el,binding,'TEXTAREA',(el)=>el.value,eventArray)
        if (binding.arg) {
          const inputRegx = /^input-(.+)$/
          if (["input"].includes(binding.arg) || inputRegx.test(binding.arg)) {
            let asyncId = undefined
            const match = binding.arg.match(inputRegx)
            if (match) asyncId = match[1]
            const uniqueId = Date.now().toString(36) + Math.random().toString(36).substr(2, 5)
            getElement(
              el,
              binding.value,
              ["TEXTAREA", "INPUT"],
              (el) => {
                if (asyncId) {
                  el.setAttribute('replace-check', asyncId)
                  el.setAttribute('empty-check', uniqueId)
                }
                return !!el.value
              }
            );
            return
          }
          const labelRegx = /^label-(.+)$/
          if (["label"].includes(binding.arg) || labelRegx.test(binding.arg)) {
            let asyncId = undefined
            const match = binding.arg.match(labelRegx)
            if (match) asyncId = match[1]
            const uniqueId = Date.now().toString(36) + Math.random().toString(36).substr(2, 5)
            getElement(
              el,
              binding.value,
              ["LABEL"],
              (el) => {
                if (asyncId) {
                  el.setAttribute('async-check', asyncId)
                  el.setAttribute('empty-check', uniqueId)
                } else {
                  el.setAttribute('empty-check', uniqueId)
                }
                return el.getAttribute('aria-checked') === 'true'
              }
            );
            return
          }
          if (["uploader"].includes(binding.arg)) {
            getElement(
              el,
              binding.value,
              // ["IMG", "INPUT"],
              ["INPUT"],
              (el) => {
                return el.checkUpload instanceof Array ? el.checkUpload.length > 0 : false
              },
              (el) => {
                el.addEventListener('evenUpload', function (event) {
                  if (!(el.checkUpload instanceof Array)) el.checkUpload = [];
                  el.checkUpload.length = event.detail
                });
              }
            );
            return
          }
        } else {
          binding.value[0].push(!!binding.value[1]);
        }
      },
    });
    app.directive("complete", {
      mounted: function (el, binding) {
        el.addEventListener("click", ()=>{
          checkEmpty(binding)
        });
      },
    });
  },
};
